package com.songkun.template.config.manager;

import android.os.Environment;

import com.songkun.template.R;
import com.songkun.template.config.AppApplication;
import com.songkun.template.config.AppController;
import com.songkun.template.utils.DataCleanManager;
import com.songkun.template.utils.FileUtil;
import com.songkun.template.utils.ILog;
import com.songkun.template.utils.UIUtils;

import java.io.File;


/**
 * 项目缓存管理类
 */
public class CacheManager {

    private static final String TAG = CacheManager.class.getCanonicalName();

    public static final String JSON_CACHE_HOME_BEAN = "json_home_bean_";

    public static final String JSON_CACHE_CITY_INFO = "json_city_info_";

    public static String getJsonCacheHomeBeanPath(int loginUserId) {
        return CacheManager.SD_CACHE_DIR + "/" + CacheManager.JSON_CACHE_HOME_BEAN + loginUserId;
    }

    public static String getJsonCacheCityInfoPath(int loginUserId) {
        return CacheManager.SD_CACHE_DIR + "/" + CacheManager.JSON_CACHE_CITY_INFO + loginUserId;
    }

    //*************有SD卡时路径******start******//
    // SD卡路径
    public static final String SD_DIR = Environment.getExternalStorageDirectory().getPath();
    // 项目路径
    public static final String SD_APP_DIR = SD_DIR + "/." + UIUtils.getString(R.string.app_name);
    // Log保存目录
    public static final String SD_LOG_DIR = SD_APP_DIR + "/.log";
    // 文件保存路径
    public static final String SD_SAVE_DIR = SD_APP_DIR + "/save";
    // 异常日志路径
    public static final String SD_CRASH_DIR = SD_APP_DIR + "/.crash";
    // 缓存路径
    public static final String SD_CACHE_DIR = SD_APP_DIR + "/.cache";
    // 图片路径
    public static final String SD_IMAGE_DIR = SD_APP_DIR + "/.image";
    // 音频路径
    public static final String SD_AUDIO_DIR = SD_APP_DIR + "/.audio";
    // 视频路径
    public static final String SD_VIDEO_DIR = SD_APP_DIR + "/video";


    private AppController controller;

    public CacheManager(AppController controller) {
        this.controller = controller;
        init();
    }

    public void init() {
        ILog.setLogDirPath(SD_LOG_DIR);

        try {
            File cacheDir = AppApplication.getInstance().getCacheDir();
            File fileDir = new File(CacheManager.SD_APP_DIR);
            long cacheSize1 = DataCleanManager.getFolderSize(cacheDir);
            long cacheSize2 = DataCleanManager.getFolderSize(fileDir);
            long cacheSize = cacheSize1 + cacheSize2;
            String cacheSizeStr = DataCleanManager.getFormatSize(cacheSize);
//            cacheSize1=65.59MB,cacheSize2=79.33KB,cacheSizeStr=65.67MB----
            ILog.e("cacheSize1=" + DataCleanManager.getFormatSize(cacheSize1) + ",cacheSize2=" + DataCleanManager.getFormatSize(cacheSize2) + ",cacheSizeStr=" + cacheSizeStr);

            if (cacheSize >= 1024 * 1024 * 1024) {//如果大于1GB就删除以前的残留文件夹
                ILog.e("删除以前的残留文件夹");
                // 删除以前的残留文件夹
                FileUtil.deleteDirectory(SD_DIR + "/.ugou");
                FileUtil.deleteDirectory(SD_DIR + "/.ugou_");
            } else {
                ILog.e("创建新的项目目录");
                //创建新的项目目录
                createProjectDir();
            }
        } catch (Exception e) {
            createProjectDir();
            e.printStackTrace();
        }
    }

    private void createProjectDir() {

        ILog.d("createProjectDir:" + FileUtil.hasSDCard());

        if (FileUtil.hasSDCard()) {

            File rootDir = new File(SD_APP_DIR);
            File logDir = new File(SD_LOG_DIR);
            File saveDir = new File(SD_SAVE_DIR);
            File crashDir = new File(SD_CRASH_DIR);
            File cacheDir = new File(SD_CACHE_DIR);
            File imageDir = new File(SD_IMAGE_DIR);
            File audioDir = new File(SD_AUDIO_DIR);
            File videoDir = new File(SD_VIDEO_DIR);

            FileUtil.mkdirs(rootDir);
            FileUtil.mkdirs(logDir);
            FileUtil.mkdirs(saveDir);
            FileUtil.mkdirs(crashDir);
            FileUtil.mkdirs(cacheDir);
            FileUtil.mkdirs(imageDir);
            FileUtil.mkdirs(audioDir);
            FileUtil.mkdirs(videoDir);

        } else {
            // do something 后续添加
            ILog.d("createProjectDir:手机无SD卡");
        }
    }

}
